package xiao_jin.fantastic_journey.client.hud;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import xiao_jin.fantastic_journey.FantasticJourney;
import xiao_jin.fantastic_journey.api.capability.Capability;
import xiao_jin.fantastic_journey.core.init.ModCapabilities;
import xiao_jin.fantastic_journey.util.ModUtil;

import java.util.concurrent.atomic.AtomicInteger;

public class Mana {
    private static final MinecraftClient MC = MinecraftClient.getInstance();

    public static final Identifier ICON = ModUtil.getIdOfMod("textures/gui/screens/hud.png");

    @SuppressWarnings("unused")
    public static void render(DrawContext context, RenderTickCounter delta) {
        assert MC.interactionManager != null;
        if (MC.getCameraEntity() == null || !(MC.getCameraEntity() instanceof PlayerEntity player)) return;

        final AtomicInteger manaValue = new AtomicInteger();
        final AtomicInteger maxMana = new AtomicInteger();

        Capability.run(ModCapabilities.MANA_CAPABILITY, player, true, (it) -> {
            manaValue.set(it.getManaValue());
            maxMana.set(it.getMaxManaValue());
            return null;
        });

        int odd = (int) Math.floor(((double) manaValue.get() / maxMana.get()) * 40);

        int x = context.getScaledWindowWidth();
        int y = context.getScaledWindowHeight();

        int xBase = x / 2 - 108;
        int yBase = y - 50;

        MC.getProfiler().push("fj_mana");
        RenderSystem.enableBlend();
        RenderSystem.setShader(GameRenderer::getPositionTexProgram);
        RenderSystem.setShaderTexture(0, ICON);
        context.drawTexture(ICON, xBase, yBase, 0, 0, 9, 48);
        context.drawTexture(ICON, xBase + 3, yBase + 3 + (40 - odd), 10, 3 + (40 - odd), 3, odd);
        RenderSystem.disableBlend();
        MC.getProfiler().pop();
    }
}
